package com.example.myproject.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 *
 *
 /*************************************************************************
 * Class: MainScreen
 * Purpose: The Main Screen Dock Panel used to display 
 * 			the program
 * 
 * **********************************************************************/
public class Habitat implements EntryPoint {

	//create a DockPanel
	DockPanel mainPanel;
	
	//add the main panel for the browser load
	public void onModuleLoad() {
		mainPanel = new DockPanel();
		mainPanel.setStyleName("backDisplay");
		mainPanel.setSize("802px","602px");
		mainPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		mainPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);		

		RootPanel.get().add(mainPanel);
		
		//check if logged in
		loggedIn();
		
		}
	
	/*************************************************************************
	 * login funciton displays login panel and checks for password
	 * 
	 * **********************************************************************/	
	public void login()
	{		
		//create login panel
		final DockPanel loginPanel = new DockPanel();
		
		loginPanel.setStyleName("loginDisplay");
		loginPanel.setSize("410px","207px");
		loginPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		loginPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);			
		
		//create textbox for password entry
		final PasswordTextBox ptb = new PasswordTextBox();
		ptb.setText("");
		
		//create a button to click that allows login
		Button loginButton = new Button("Login");
		
		//add the textbox and button to the login panel and 
		//then add the login panel to the main panel
		Label pass = new Label();
		pass.setText("Password:      ");
		loginPanel.add(loginButton, DockPanel.SOUTH);		
		loginPanel.add(ptb, DockPanel.SOUTH);
		loginPanel.add(pass, DockPanel.SOUTH);
		mainPanel.add(loginPanel, DockPanel.CENTER);
		
		ptb.setFocus(true);
		
		//create the click event for the login button
		//set the password text box only able to contain "cs5761"
		//if successful, call loggedIn otherwise reset the textbox
		//for next entry
		loginButton.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	            if(ptb.getText().equals("cs4531"))
	            {
	            	//remove login panel and log-in
	            	mainPanel.remove(loginPanel);
	            	loggedIn();	            	
	            }
	            else
	            {
	            	ptb.setText("");
	            }
	        }
	    });			
	}
	
	/*************************************************************************
	 * You have succesfully logged in, now add a main panel to the screen
	 * **********************************************************************/
	
	public void loggedIn()
	{
		//create main screen and add to the main panel
		MainScreen main = new MainScreen();
		
		//add it
		mainPanel.add(main, DockPanel.CENTER);
	}

}
